<?php



if(!function_exists('success')){
    function success($data=[]){
        return [
            'code'=>1,
            'message'=>'OK',
            'data'=>$data
        ];
    }
}

if(!function_exists('error')){
    function error($data = [], $msg = 'error', $code = 0) {
        return [
            'code'    => $code,
            'message' => $msg,
            'data'    => $data
        ];
    }
}


if(!function_exists('objectToArray')){
    function objectToArray($object) {
        return json_decode(json_encode($object), true);
    }
}



/**
* 递归实现无限极分类
* @param $array 分类数据
* @param $pid 父ID
* @param $level 分类级别
* @return $list 分好类的数组 直接遍历即可 $level可以用来遍历缩进
*/
function getTree($array, $pid =0 ){
    $list = array();
    foreach ($array as $v1){
        $v1=[
            'id'=>$v1['id'],
            'parent_id'=>$v1['parent_id'],
            'name'=>$v1['name'],
            'type'=>$v1['type'],
            'icon'=>$v1['icon'],
            'path'=>$v1['path'],
            'code'=>$v1['code'],
        ];
      if ($v1['parent_id'] == $pid){
        foreach($array as $v2){
            if($v2['parent_id']==$v1['id']){
                $v1['children']=getTree($array,$v1['id']);
                break;
            }else{
            }
        }
        $list[]=$v1;
      }
    }
    return $list;
}